﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Npgsql;

namespace posmoon.Tree
{
    public class DBTreeNode : TreeNode
    {
        #region Attributes
        
        private string _name;
        private string _pass;
        private string _user;
        private HostTreeNode _host;
        private NpgsqlConnection _conn;
        private EncryptDecrypt _encr;

        #endregion

        public DBTreeNode(string name, string user, string pass, HostTreeNode host) : base(name)
        {
            _name = name;
            _user = user;
            _pass = pass;
            _host = host;

            this.ForeColor = SystemColors.GrayText;
            _encr = new EncryptDecrypt();

            InitializeConnection();
        }

        private void InitializeConnection()
        {
            string connstr = "Server=" + _host.HostIP +
                            ";Port=" + _host.HostPort +
                            ";User Id=" + _user +
                            ";Password=" + _pass +
                            ";Database=" + _name + ";";

            _conn = new NpgsqlConnection(connstr);
        }

        public void Register()
        {
            try
            {
                _conn.Open();

                if (_conn.State == System.Data.ConnectionState.Open)
                {
                    new RegistrytSaver().WriteValue(_host.HostName + "\\" + _name, 
                                                    _encr.GetEncrypted(_user), 
                                                    _encr.GetEncrypted(_pass));
                }
            }
            catch
            {

            }
        }

        public void Reader()
        {

        }

        private void WriteToRegistry()
        {

        }
    }
}
